/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gtracker;

import java.util.Vector;
import java.util.Hashtable;

/**
 *
 * @author sajin
 */
public class GroupInfo {

	public GroupInfo() {
		fileTable = new Hashtable<String, Vector<String>>();
		userList = new Vector<String>();
	}

	public boolean isMemberOf(String userName) {
		for (String user : userList) {
			if (user.equals(userName)) {
				return true;
			}
		}

		return false;
	}

	public void removeUser(String userName) {
		if (isMemberOf(userName)) {
			synchronized (this) {
				userList.remove(userName);

				for (String fileNames : fileTable.keySet()) {
					fileTable.get(fileNames).remove(userName);
				}
			}
		}
	}

	public boolean fileHasSeeder(String sha1FileName) {
		if (fileTable.get(sha1FileName) != null) {
			return true;
		}

		return false;
	}

	public Vector<String> getPeers(String sha1FileName) {
		return fileTable.get(sha1FileName);
	}

	public void addAsPeer(String userName, String sha1FileName) {
		if (fileHasSeeder(sha1FileName)) {
			synchronized(this) {
				fileTable.get(sha1FileName).add(userName);
			}
		} else {
			Vector<String> uList = new Vector<String>();
			uList.add(userName);
			synchronized(this ) {
				fileTable.put(sha1FileName, uList);
			}
		}
	}

    public Vector<String> getGroupUserList() {
        return userList;
    }
	public Hashtable<String, Vector<String>> fileTable;
	public Vector<String> userList;
}
